/************************************************************************
                 gmDisplaySetup.cpp - Copyright Claudio Botta

This library is free software; you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or 
(at your option) any later version.
This library is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public License 
along with this library; if not, write to the 
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 
Boston, MA 02110, USA

**************************************************************************/

#include "gmDisplaySetup.h"
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include <stdlib.h>



using namespace std;

gmDisplay::gmDisplay()
{
        this->resx = 800;
        this->resy = 600;
        this->colorDepth = 24;
}

int gmDisplay::init()
{
        this->init(this->resx, this->resy, this->colorDepth);
}

int gmDisplay::init(int x, int y, int depth)
{

        if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1) {
                cout << "could not initialize SDL Video\n";
                return -1;
        } else {
                this->isInitialized = true;
        }

       
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
  SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0);
  SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0);
  SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0);
  SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
  SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);

  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);      //Use at least 5 bits of Red
  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);    //Use at least 5 bits of Green
  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);     //Use at least 5 bits of Blue
  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);   //Use at least 16 bits for the depth buffer
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);  //Enable double buffering
        this->resx = x;
        this->resy = y;
        this->colorDepth = depth;
        this->screen = SDL_SetVideoMode( x, y, depth, SDL_OPENGL | SDL_HWSURFACE );
	

        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        //glOrtho( 0.0f, 1024.0f, 768.0f, 0.0f,+10000.0f, -100000.0f );
	gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,2000.0);
        glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
        glClearDepth( 2.0f );
        glEnable( GL_DEPTH_TEST );
        glDepthFunc( GL_LEQUAL );
	
	
		glMatrixMode(GL_MODELVIEW);
        	glViewport( 0, 0, 1024, 768);
		glPointSize(1024/200.0);


}


int gmDisplay::setRes(int x, int y, int depth)
{
        if (this->isInitialized) {
                this->screen = SDL_SetVideoMode( x, y, depth, SDL_OPENGL | SDL_HWSURFACE );
                this->resx = x;
                this->resy = y;
                this->colorDepth = depth;
        }
}
